package Z11_20_pool;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author： pang-yun
 * @date： 2021-11-20 17:07
 * 功能目的 ：用于 任务调度应用  demo  如何每周四 18:00:00 定时执行任务？
 * <p>
 * Runnable command,     具体要执行的操作
 * long initialDelay,    延迟多长时间后执行
 * long period,          每次执行之间的间隔
 * TimeUnit unit         时间单位
 */

public class FixedTimePool {
    public static void main(String[] args) {
        ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);

        // 获取 时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        // 获取周日时间
        LocalDateTime thursDay = now.withHour(18).withMinute(01).withSecond(0).withNano(0).with(DayOfWeek.SATURDAY);
        // 如果 当前时间 已经 大于 本周周四  那么 要找的应该是 下周周四
        if (now.compareTo(thursDay) > 0) {
            thursDay = thursDay.plusWeeks(1);
        }
        System.out.println(thursDay);
        //获得延迟时间
        System.out.println(Duration.between(now, thursDay).toDays());

        long initialDelay = Duration.between(now, thursDay).toMillis();
        long period = 1000 * 5;

        pool.scheduleAtFixedRate(()->{
            System.out.println(LocalDateTime.now());
        },initialDelay,period, TimeUnit.MILLISECONDS);

    }
}